package edu.uclmgmu.bacteriosec.aspects.common;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;

import edu.uclmgmu.bacteriosec.domain.FiltroGenerico;

public class FilesStore {
	/***
	 * Executed by *_CountCalls
	 * @param signature
	 */
	public static void updateNumberOfCalls(String joinPointName) {
		String fileName=System.getProperty("java.io.tmpdir") + joinPointName + ".numberOfCalls";
		int numberOfCalls=1;
		try {
			DataInputStream dis=new DataInputStream(new FileInputStream(fileName));
			numberOfCalls=dis.readInt();
			dis.close();
			numberOfCalls+=1;
		}
		catch (FileNotFoundException e) {
		}
		catch(Exception e) {
			System.out.println(e.toString());
		}	
		try {
			DataOutputStream dos=new DataOutputStream(new FileOutputStream(fileName));
			dos.writeInt(numberOfCalls);
			dos.close();
			// TODO : borrar
			FileOutputStream fos=new FileOutputStream(fileName + ".txt");
			fos.write((""+numberOfCalls).getBytes());
			fos.close();
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}

	public static int getNumberOfCallInTurn(String joinPointName) {
		String fileName=System.getProperty("java.io.tmpdir") + joinPointName + ".numberOfCallInTurn";
		int result=-1;
		try {
			DataInputStream dis=new DataInputStream(new FileInputStream(fileName));
			result=dis.readInt();
			dis.close();

		}
		catch(Exception e) {
			System.out.println(e.toString());
		}
		return result;
	}

	public static int getNumberOfCurrentCall(String joinPointName) {
		String fileName=System.getProperty("java.io.tmpdir") + joinPointName + ".numberOfCurrentCall";
		int result=-1;
		FileNotFoundException fnfe=null;
		try {
			DataInputStream dis=new DataInputStream(new FileInputStream(fileName));
			result=dis.readInt();
			dis.close();
		}
		catch(FileNotFoundException e){
			fnfe = e;
		}
		catch(IOException e) {
			System.out.println("Error en getNumberOfCurrentCall " + e.toString());
		}
		if (fnfe!=null) {
			try {
				DataOutputStream dos=new DataOutputStream(new FileOutputStream(fileName));
				dos.writeInt(1);
				dos.close();
				result=1;
				// TODO: borrar
				
			} catch (Exception e) {
				System.out.println(e.toString());
			}
		}
		return result;
	}

	public static void updateNumberOfCurrentCall(String joinPointName) {
		String fileName=System.getProperty("java.io.tmpdir") + joinPointName + ".numberOfCurrentCall";
		try {
			DataInputStream dis=new DataInputStream(new FileInputStream(fileName));
			int currentCall=dis.readInt();
			dis.close();
			currentCall++;
			
			DataOutputStream dos=new DataOutputStream(new FileOutputStream(fileName));
			dos.writeInt(currentCall);
			dos.close();
			// TODO: borrar
			FileOutputStream auxi=new FileOutputStream(fileName+".txt");
			auxi.write((""+currentCall).getBytes());
			auxi.close();
		}
		catch(Exception e) {
			//System.out.println("ERRORZACO: " + e.toString());
		}	
	}

	public static void log(String canonicalName, int numberOfCurrentCall, String signature) {
//		String text="Operator: " + canonicalName + "\nCurrent call: " + numberOfCurrentCall + "\nJoin point:" + signature + "\n\n";
//		System.out.println(text);
	}

	public static String getCurrentjoinPoint() {
		String fileName=System.getProperty("java.io.tmpdir") + "currentJoinPoint.txt";
		String result=null;
		try {
			FileInputStream dis=new FileInputStream(fileName);
			byte[] b=new byte[dis.available()];
			dis.read(b);
			dis.close();
			result=new String(b);
		}
		catch(Exception e) {
			System.out.println("Error leyendo currentJoinPoint.txt-> " + e.toString());
		}
		return result;
	}

	public static int getNumberOfCallsToJP(String jpFile) {
		try {
			DataInputStream dis=new DataInputStream(new FileInputStream(jpFile + ".numberOfCalls"));
			int numberOfCallsToJP=dis.readInt();
			dis.close();
			return numberOfCallsToJP;
		} catch (Exception e) {
			return -1;
		}
	}

	public static void saveCurrentJoinPoint(String jpName) {
		String tempPath=System.getProperty("java.io.tmpdir");
		try {
			FileOutputStream fos=new FileOutputStream(tempPath + "currentJoinPoint.txt");
			fos.write(jpName.getBytes());
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void createFileForCallInTurn(String joinPointName, int callInTurn) throws IOException {
		String fileName=joinPointName + ".numberOfCallInTurn";
		DataOutputStream dos=new DataOutputStream(new FileOutputStream(fileName));
		dos.writeInt(callInTurn);
		dos.close();
		// TODO: borrar
		FileOutputStream auxi=new FileOutputStream(fileName+".txt");
		auxi.write((""+callInTurn).getBytes());
		auxi.close();
	}

	public static void saveAOP_AJC_XML(String aopXMLPath, String text) throws IOException {
		FileOutputStream fos=new FileOutputStream(aopXMLPath);
		fos.write(text.getBytes());
		fos.close();
	}

	public static int readNumberOfCalls(String jpName) throws IOException {
		DataInputStream dis=new DataInputStream(new FileInputStream(jpName + ".numberOfCalls"));
		int n=dis.readInt();
		dis.close();
		return n;
	}
	
	public static void createFileForCurrentCall(String joinPointName, int currentCall) throws IOException {
		String fileName=joinPointName + ".numberOfCurrentCall";
		DataOutputStream dos=new DataOutputStream(new FileOutputStream(fileName));
		dos.writeInt(currentCall);
		dos.close();
		// TODO: borrar
		FileOutputStream auxi=new FileOutputStream(fileName+".txt");
		auxi.write((""+currentCall).getBytes());
		auxi.close();
	}

	public static Vector<String> loadFilesNumberOfCalls() {
		String tempPath=System.getProperty("java.io.tmpdir");
		FiltroGenerico fg=new FiltroGenerico();
		Vector<String> jpFiles=fg.loadFiles(tempPath, ".numberOfCalls");
		Vector<String> result=new Vector<String>();
		for (String jpFileName : jpFiles) {
			jpFileName=jpFileName.substring(0, jpFileName.length()-".numberOfCalls".length());
			result.add(jpFileName);
		}
		return result;
	}
}
